package org.standard.commons.tlds;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Locale;
import java.util.Properties;

import org.apache.log4j.helpers.Loader;

public class PropertiesFileConstants extends Properties {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 9009195426056762176L;
	
	private static final String propertiesFileName = "globalVariables";
	private static final String propertiesFileSuffix = ".properties";
	
	
	private PropertiesFileConstants(){
		
	}
	
	private static PropertiesFileConstants constants = null;
	
	
	
	synchronized public static PropertiesFileConstants getPropertiesFileConstants(Locale locale) {
			
		if (locale == null) {
			locale = Locale.CHINA;
		}
		if(constants == null){
			
			constants = new PropertiesFileConstants();
			String fileName = propertiesFileName + "_" + locale.toString() + propertiesFileSuffix;
			
			URL url = Loader.getResource(fileName);
			
	        InputStreamReader is;
			try {
				is = new InputStreamReader(url.openStream(), "UTF-8");
				BufferedReader br = new BufferedReader(is);  
				constants.load(br);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				System.out.println("file ["+fileName+"] is not found");
				e.printStackTrace();
			}   
		}
        
		return constants;
	}
	
	synchronized public static PropertiesFileConstants getPropertiesFileConstants() {
		
		return getPropertiesFileConstants(null);
	}
	public void init() {
		getPropertiesFileConstants(null);
	}

}
